package com.yeban.javase.day17.test.test4;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/31
 *
 * new ThreadPoolExecutor.DiscardOldestPolicy()
 *
 * corePoolSize  2
 * maxPoolSize   3
 * queueSize     3
 */
public class ThreadPoolExecutorDemo {

    public static void main(String[] args) {
        CustomThreadPoolDemo1 pool = new CustomThreadPoolDemo1(2, 3);
        /**
         * 执行任务的顺序
         */
        pool.exec(new MyRunnable());
        pool.exec(new MyRunnable());
        pool.exec(new MyRunnable());
        pool.exec(new MyRunnable());
        pool.exec(new MyRunnable());
        pool.exec(new MyRunnable());
        pool.exec(new MyRunnable());
    }
}

class CustomThreadPoolDemo1 {
    private ExecutorService pool;
    public CustomThreadPoolDemo1(int corePoolSize, int maximumPoolSize) {
        pool = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(3),
                new ThreadPoolExecutor.DiscardOldestPolicy ()
        );
    }
    // 执行任务方法
    public void exec(Runnable runnable) {
        pool.execute(runnable);
        // pool.submit(runnable);
    }
    // 关闭线程池
    public void close() {
        pool.shutdown();
    }
}
